package com.xnyzc.lhy.mis.service.impl.worksheet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.worksheet.WorksheetGetOutgoingPhoneNumberParam;
import com.xnyzc.lhy.mis.entity.param.worksheet.didi.BindVirtualNumberParam;
import com.xnyzc.lhy.mis.entity.param.worksheet.didi.CreateWorkParam;
import com.xnyzc.lhy.mis.entity.param.worksheet.didi.UnbundleVirtualNumberParam;
import com.xnyzc.lhy.mis.entity.param.worksheet.softbar.CallPhoneResult;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.worksheet.OaSysWorksheet;
import com.xnyzc.lhy.mis.entity.worksheet.OaSysWorksheetCall;
import com.xnyzc.lhy.mis.feign.didi.WorkService;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.mapper.worksheet.OaSysWorksheetCallMapper;
import com.xnyzc.lhy.mis.mapper.worksheet.OaSysWorksheetMapper;
import com.xnyzc.lhy.mis.mapper.worksheet.UserInfoMapper;
import com.xnyzc.lhy.mis.service.worksheet.IOaSysWorksheetCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 工单呼叫记录 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-11-04
 */
@Service
public class OaSysWorksheetCallServiceImpl extends ServiceImpl<OaSysWorksheetCallMapper, OaSysWorksheetCall> implements IOaSysWorksheetCallService {

    @Autowired
    OaSysWorksheetCallMapper oaSysWorksheetCallMapper;

    @Autowired
    OaSysWorksheetMapper oaSysWorksheetMapper;

    @Autowired
    OaDUserMapper oaDUserMapper;

    @Autowired
    WorkService workService;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public Rv insertIOaSysWorksheetCall(OaSysWorksheetCall oaSysWorksheetCall) {
        oaSysWorksheetCall.setCreateTime(new Date());
        oaSysWorksheetCallMapper.insert(oaSysWorksheetCall);
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 电话条外呼虚拟号获取
     * @param worksheetGetOutgoingPhoneNumberParam
     * @return
     */
    @Override
    public DiDiRv queryGetOutgoingPhoneNumber(WorksheetGetOutgoingPhoneNumberParam worksheetGetOutgoingPhoneNumberParam) {

        CallPhoneResult callPhoneResult  = new CallPhoneResult();
        OaSysWorksheetCall oaSysWorksheetCall = new OaSysWorksheetCall();
        String  type = worksheetGetOutgoingPhoneNumberParam.getCall_type();
        OaSysWorksheet oaSysWorksheet = oaSysWorksheetMapper.selectOne(Wrappers.<OaSysWorksheet>lambdaQuery()
                .eq(OaSysWorksheet::getHeliWorksheetId, worksheetGetOutgoingPhoneNumberParam.getTicket_no()));

        if(oaSysWorksheet != null){
            if(type.equals("2")){
                BindVirtualNumberParam bindVirtualNumberParam = new BindVirtualNumberParam();
                bindVirtualNumberParam.setCall_type(Integer.parseInt(worksheetGetOutgoingPhoneNumberParam.getCall_type()));
                bindVirtualNumberParam.setTicket_no(oaSysWorksheet.getOrderId());
                bindVirtualNumberParam.setCusPhone(worksheetGetOutgoingPhoneNumberParam.getPhoneNumber());
                DiDiRv diDiRv1 = workService.getBindNumber(bindVirtualNumberParam);

                Map<String,Object> map1 = JSONObject.parseObject(JSON.toJSONString(diDiRv1.getData()));
                oaSysWorksheetCall.setPhone(map1.get("phone_number").toString());
                oaSysWorksheetCall.setBindId(map1.get("bind_id").toString());

                callPhoneResult.setPhone(map1.get("phone_number").toString());

            }else if(type.equals("1")){
                String driverPhone = userInfoMapper.getDriverPhone(oaSysWorksheet.getOrderId());

                if(driverPhone == null
                        || "".equals(driverPhone))
                {
                    return DiDiRv.wrap(EDiDiErrorCode.parameterError,"没有获取到司机的手机号");
                }

                callPhoneResult.setPhone(driverPhone);

                oaSysWorksheetCall.setPhone(driverPhone);
            }else if(type.equals("3")){
                callPhoneResult.setPhone(oaSysWorksheet.getFeedbackPhone());
                oaSysWorksheetCall.setPhone(oaSysWorksheet.getFeedbackPhone());
            }
        }else{
            return DiDiRv.wrap(EDiDiErrorCode.parameterError,"工单不存在");
        }
        oaSysWorksheetCall.setCallId(IDUtil.nextId());
        oaSysWorksheetCall.setWorksheetId(oaSysWorksheet.getWorksheetId());
        oaSysWorksheetCall.setStartTime(new Date());
        oaSysWorksheetCall.setCreateUser(TokenUtil.getTokenUserId());
        oaSysWorksheetCall.setCreateTime(new Date());
        oaSysWorksheetCallMapper.insert(oaSysWorksheetCall);

        callPhoneResult.setCallId(oaSysWorksheetCall.getCallId().toString());
        return DiDiRv.wrap(EDiDiErrorCode.success, callPhoneResult);
    }

    /**
     * 挂电话
     * @param callId
     * @return
     */
    @Override
    public Rv hangUp(String callId) {
        OaSysWorksheetCall oaSysWorksheetCall = oaSysWorksheetCallMapper.selectById(callId);
        if(oaSysWorksheetCall != null){
            if(oaSysWorksheetCall.getBindId() != null){
                UnbundleVirtualNumberParam unbundleVirtualNumberParam = new UnbundleVirtualNumberParam();
                unbundleVirtualNumberParam.setCall_id(callId);
                workService.getUnbundleNumber(unbundleVirtualNumberParam);
            }
        }else{
            return Rv.wrap(EErrorCode.noData,"未查询到此工单记录");
        }
        oaSysWorksheetCall.setUpdateTime(new Date());
        oaSysWorksheetCall.setUpdateUser(TokenUtil.getTokenUserId());
        oaSysWorksheetCall.setEndTime(new Date());
        oaSysWorksheetCallMapper.updateById(oaSysWorksheetCall);
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    public Rv getUserInfo() {
        OaDUser oaDUser = oaDUserMapper.selectById(TokenUtil.getTokenUserId());
        Map map = new HashMap();
        // map.put("heli_phone_username",oaDUser.getHeliPhoneUsername());
        // map.put("heli_phone_password",oaDUser.getHeliPhonePassword());
        return Rv.wrap(EErrorCode.success,map);
    }
}
